Haunted Creature with Lights and Sounds

This project was generated by with help from ChatGPT so it may not be perfect!

Project Overview

In this project, you will create a spooky Halloween creature using creative materials and bring it to life with an RGB LED and a buzzer using a Raspberry Pi Pico. The creature will light up in spooky colors and make eerie sounds when a button is pressed!

Materials Needed

Step-by-Step Instructions

1. Set Up the Circuit

Follow these steps to wire the RGB LED, buzzer, and button to the Raspberry Pi Pico:

2. Write the Code in Thonny

2.1 Setting Up the RGB LED, Buzzer, and Button

First, you'll set up the pins for the RGB LED, buzzer, and button. This tells the Raspberry Pi Pico which GPIO pins are connected to each component.

from machine import Pin, PWM
import time

# Set up the RGB LED pins
red = PWM(Pin(15))
green = PWM(Pin(14))
blue = PWM(Pin(13))

# Set up the Buzzer pin
buzzer = PWM(Pin(16))

# Set up the Button pin
button = Pin(17, Pin.IN, Pin.PULL_DOWN)
            

Here, the RGB LED's red, green, and blue pins are connected to GPIO 15, 14, and 13 respectively. The buzzer is connected to GPIO 16, and the button to GPIO 17 with a pull-down resistor to ensure it's in a low state when not pressed.

2.2 Writing Functions for the RGB LED and Buzzer

Next, you'll write two functions. One for setting the color of the RGB LED, and another for making the buzzer play a sound at a given frequency for a set duration.

# Function to set the RGB color
def set_color(r, g, b):
    red.duty_u16(r)
    green.duty_u16(g)
    blue.duty_u16(b)

# Function to play a tone on the buzzer
def play_tone(frequency, duration):
    buzzer.freq(frequency)
    buzzer.duty_u16(1000)
    time.sleep(duration)
    buzzer.duty_u16(0)
            

The set_color function takes three parameters—red, green, and blue—and sets the brightness of each LED channel. The play_tone function sets the buzzer's frequency and plays a sound for the specified duration before turning the buzzer off.

2.3 Defining Color Values

Next, you'll define some spooky colors as a dictionary of RGB values, each ranging from 0 (off) to 65535 (fully on) in 16-bit PWM.

# Colors (16-bit PWM values)
colors = {
    'red': (65535, 0, 0),
    'green': (0, 65535, 0),
    'blue': (0, 0, 65535),
    'purple': (32000, 0, 32000),
    'orange': (65535, 32768, 0)
}
            

These color definitions allow you to easily reference spooky colors like "purple" and "orange" in your code later.

2.4 Main Loop to Respond to Button Presses

Finally, you'll write the main loop, which checks if the button is pressed. If it is, the RGB LED turns purple, the buzzer plays a tone, and the LED turns off again.

# Main loop
while True:
    if button.value() == 1:  # Button is pressed
        set_color(*colors['purple'])  # Set spooky purple color
        play_tone(600, 0.5)  # Play a creepy sound for 0.5 seconds
        set_color(0, 0, 0)  # Turn off the light
        time.sleep(1)
    else:
        set_color(0, 0, 0)  # LED off when button isn't pressed
        buzzer.duty_u16(0)  # Buzzer off
            

In this loop, the button's state is checked continuously. When pressed, it triggers the purple light and spooky sound. After the action, it waits a second before checking again.

3. Upload the Code

Save the file as main.py in Thonny and upload it to the Pico by selecting File > Save As and choosing the Raspberry Pi Pico as the location.

4. Build Your Haunted Creature

Now that the electronics work, use creative materials to build a spooky creature! Position the RGB LED for glowing eyes and hide the buzzer inside for spooky sound effects. Get creative and give your creature a spooky personality!